package com.qiongqi.datadir.config;


import com.bettercloud.vault.SslConfig;
import com.bettercloud.vault.Vault;
import com.bettercloud.vault.VaultConfig;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.Serializable;

/**
 *
 * @author wmao
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "vault")
public class VaultClientConfig implements Serializable {

    /**
     * 访问地址
     */
    private String url;

    /**
     * 用户名
     */
    private String token;

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).toString();
    }

    @Bean
    public Vault vaultClient() throws Exception {
        final VaultConfig config =
                new VaultConfig()
                        .address(url)               // Defaults to "VAULT_ADDR" environment variable
                        .token(token)  // Defaults to "VAULT_TOKEN" environment variable
                        .openTimeout(5)                                 // Defaults to "VAULT_OPEN_TIMEOUT" environment variable
                        .readTimeout(30)                                // Defaults to "VAULT_READ_TIMEOUT" environment variable
                        .engineVersion(2)
                        .sslConfig(new SslConfig().build())             // See "SSL Config" section below
                        .build();
        return new Vault(config);
    }
}